package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Module;
import cn.itcast.service.system.ModuleService;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/module")
public class ModuleController extends BaseController {

    @Autowired
    private ModuleService moduleService;

    @RequestMapping(value = "/list",name = "查询所有模块信息")
    public String findAll(HttpServletRequest request, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Module> pageInfo = moduleService.findByPage(page, pageSize);
        request.setAttribute("page", pageInfo);
        return "system/module/module-list";
    }

    @RequestMapping(value = "toAdd",name = "跳转到新增部门页面")
    public String add() {
        List<Module> moduleList = moduleService.findAll();
        request.setAttribute("menus", moduleList);
        return "system/module/module-add";
    }

    @RequestMapping(value = "/toUpdate",name = "跳转到修改部门页面")
    public String toUpdate(String id) {
        Module module = moduleService.findById(id);
        request.setAttribute("module", module);

        List<Module> moduleList = moduleService.findAll();
        request.setAttribute("menus", moduleList);
        return "system/module/module-update";
    }

    @RequestMapping(value = "edit",name = "保存部门信息")
    public String save(Module module) {
        if (StringUtils.isEmpty(module.getId())) {
            String id = UUID.randomUUID().toString();
            module.setId(id);
            moduleService.save(module);
        } else {
            moduleService.updateById(module);
        }
        return "redirect:/system/module/list.do";
    }

    @RequestMapping(value = "delete",name = "删除部门信息")
    public String deleteById(String id) {
        moduleService.deleteById(id);
        return "redirect:/system/module/list.do";
    }
}
